package hu.myai.indicators.tick;

import hu.myai.api.indicators.AbstractIndicator;
import hu.myai.model.IndicatorVO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Indicator returns the candle parameter; open, close, high, low, center or
 * feel
 *
 * @author bozbalint
 *
 */
public class TickIndicator extends AbstractIndicator {

    public final static double OPEN = 0.0;
    public final static double CLOSE = 1.0;
    public final static double HIGH = 2.0;
    public final static double LOW = 3.0;
    public final static double CENTER = -1.0;
    public final static double PIVOTPOINT = -2.0;
    public final static double FEEL = -3.0;

    /**
     * The object is used when logging debug,errors,warnings and info.
     * */
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    /**
     * Constructor
     */
    public TickIndicator(Double parameter) {
        super(parameter);
    }

    @Override
    public void init() {
    }

    @Override
    protected IndicatorVO indicatorCalculation() {

        IndicatorVO indicatorVO = new IndicatorVO();

        if (parameter == OPEN)
            indicatorVO.setValue(lastTick.getOpen());
        else if (parameter == CLOSE)
            indicatorVO.setValue(lastTick.getClose());
        else if (parameter == HIGH)
            indicatorVO.setValue(lastTick.getHigh());
        else if (parameter == LOW)
            indicatorVO.setValue(lastTick.getLow());
        else

        // center of gravity
        if (parameter == CENTER)
            indicatorVO
                    .setValue((lastTick.getHigh() + lastTick.getLow() + lastTick.getOpen() + lastTick.getClose()) / 4);
        else

        // center of gravity
        if (parameter == PIVOTPOINT)
            indicatorVO.setValue((lastTick.getHigh() + lastTick.getLow() + lastTick.getClose()) / 3);
        else

        // how far did it go
        if (parameter == FEEL) {
            boolean isUp = lastTick.getOpen() > lastTick.getClose();
            indicatorVO.setValue(isUp ? lastTick.getHigh() : lastTick.getLow());
        }

        indicatorVO.setDate(lastTick.getTime());

        log.debug("IndivatorVO: " + indicatorVO.toString());
        return indicatorVO;
    }
}
